package com.fanrui.code.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.ibatis.session.SqlSession;

import com.fanrui.code.db.jvmdb.JvmDao;
import com.fanrui.code.entity.TableLog;
import com.fanrui.code.entity.TableLogMapper;
import com.fanrui.code.thread.MainLoopPlus.StrongRunnable;
import com.fanrui.code.util.SessionUtil;
import com.fanrui.code.util.core.LoopThreadPool;
import com.fanrui.code.util.logger.Stdout;

public class TableLogDao extends JvmDao<TableLog, Integer> {

	// singleton
	private TableLogDao() {
		super(TableLog.class);
	}

	private static class TableLogDaoSingleton {
		private static TableLogDao instance = new TableLogDao();
	}

	public static TableLogDao getInstance() {
		return TableLogDaoSingleton.instance;
	}

	public void startSave() {
		LoopThreadPool.getInstance().scheduleAtFixedRate(new StrongRunnable() {

			@Override
			protected void strongRun() throws Exception {
				updateToDB();
			}

		}, 5, 5, TimeUnit.SECONDS);
	}

	/**
	 * 定时更新需要修改的数据到数据库
	 * 
	 * @throws Exception
	 */
	public void updateToDB() throws Exception {
		if (modifiedEntities != null && !modifiedEntities.isEmpty()) {
			synchronized (modifiedEntities) {
				Stdout.GET().info("TableLogDao:定时更新数据到数据库!此次更新{}条数据", modifiedEntities.size());
				SqlSession session = SessionUtil.getSession();
				TableLogMapper um = session.getMapper(TableLogMapper.class);
				List<TableLog> logList = new ArrayList<>(modifiedEntities.keySet());
				if (logList != null && !logList.isEmpty()) {
					for (TableLog tableLog : logList) {
						um.insert(tableLog);
					}
				}
				session.commit();
				session.close();
				modifiedEntities.clear();

				// 查询是否添加到了数据库
				for (TableLog log : logList) {
					if (queryLogByToken(log.getPlayerId(), log.getLogToken()) == null) {
						throw new Exception("添加tablelog失败！房间号：" + log.getTableId() + "玩家ID：" + log.getPlayerId() + "TOKEN：" + log.getLogToken());
					}
				}
			}
		}

	}

	@Override
	protected Collection<TableLog> dbFindAll() {
		return null;
	}

	@Override
	protected void OtherSave4jvm(TableLog entity) {

	}

	@Override
	protected void otherDelete4Jvm(TableLog entity) {

	}

	@Override
	protected void dbSave(TableLog t) {

	}

	@Override
	protected void dbDelete(Integer entityId) {

	}

	public TableLog queryLogByToken(Long playerId, String logToken) {
		try (SqlSession session = SessionUtil.getSession()) {
			TableLogMapper um = session.getMapper(TableLogMapper.class);
			return um.queryLogByToken(playerId, logToken);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<TableLog> selectTablLogs(List<Integer> tableIds){
		try(SqlSession session = SessionUtil.getSession()) {
			TableLogMapper um = session.getMapper(TableLogMapper.class);
			return um.selectTableLogs(tableIds);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<Integer> getPlayerJoinTableIds(Long playerId, String data){
		try(SqlSession session = SessionUtil.getSession()) {
			TableLogMapper um = session.getMapper(TableLogMapper.class);
			return um.selectTabldIsByPlayerID(playerId ,data);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public List<Integer> getClubTableIds(Integer clubId, String data){
		try(SqlSession session = SessionUtil.getSession()) {
			TableLogMapper um = session.getMapper(TableLogMapper.class);
			return um.selectTabldIsByClubID(clubId ,data);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
